/*
 * c7_exercise9.cpp
 *
 *  Created on: Sep 2, 2013
 *      Author: cyper.yin
 */

#include <iostream>
#include <cstring>
using namespace std;
const int SLEN = 30;

struct student {
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};

int getinfo(student pa[], int n);
void display1(student st);
void display2(const student * ps);
void display3(const student pa[], int n);
bool isBlankString(const char str[], int n);

int main_c7e9(int argc, char **argv) {
	cout << "Enter class size:";
	int class_size;
	cin >> class_size;
	while (cin.get() != '\n') {
		continue;
	}

	student * ptr_stu = new student[class_size];
	int entered = getinfo(ptr_stu, class_size);
	for (int i = 0; i < entered; i++) {
		cout << "========= display 1 ===============" << endl;
		display1(ptr_stu[i]);

		cout << "========= display 2 ===============" << endl;
		display2(&ptr_stu[i]);
	}

	cout << "========= display 3 ===============" << endl;
	display3(ptr_stu, entered);

	delete[] ptr_stu;
	cout << "Done\n";

	return 0;
}
int getinfo(student pa[], int n) {

	for (int i = 0; i < n; i++) {
		cout << "Enter student #" << i << endl;

		cout << "fullname:";
		char temp[SLEN];
		cin.getline(temp, SLEN);
		//quit for blank line
		if (isBlankString(temp, strlen(temp))) {
			return i;
		} else {
			strcpy(pa[i].fullname, temp);
		}

		cout << "hobby:";
		cin.getline(pa[i].hobby, SLEN);

		cout << "ooplevel:";
		cin >> pa[i].ooplevel;

		cin.get();
	}

	return n;
}
void display1(student st) {
	cout << "student name:" << st.fullname << endl;
	cout << "student hobby:" << st.hobby << endl;
	cout << "student ooplevel:" << st.ooplevel << endl;

}
void display2(const student * ps) {
	cout << "student name:" << ps->fullname << endl;
	cout << "student hobby:" << ps->hobby << endl;
	cout << "student ooplevel:" << ps->ooplevel << endl;
}

void display3(const student pa[], int n) {
	for (int i = 0; i < n; i++) {
		display2(&pa[i]);
	}
}

bool isBlankString(const char str[], int n) {
	if (n == 0) {
		return true;
	}

	for (int i = 0; i < n; i++) {
		if (!isblank(str[i])) {
			return false;
		}
	}
	return true;
}
